/*
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.chimos.property.compiler.passone;

import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ASM4;

import java.util.List;

import org.objectweb.asm.AnnotationVisitor;

import de.chimos.property.compiler.Config;
import de.chimos.property.compiler.PropertyInfo;

/**
 * @author Niklas Hofmann
 */
public class PassOneMethodAnnotationVisitor extends AnnotationVisitor {
    
    private final String className;
    private String propertyName;
    
    private String signature;
    private String signatureGeneric;

    private String humanReadablePropertyName = null;
    
    private final List<PropertyInfo> properties;
    
    @SuppressWarnings("unused")
	private final Config config;
    
    public PassOneMethodAnnotationVisitor(String className, List<PropertyInfo> properties, Config config)
    {
        super(ASM4);
        
        this.className = className;
        
        this.properties = properties;
        
        this.config = config;
    }

    @Override
    public void visit(String name, Object value)
    {
    	switch(name)
    	{
    	case "name":
    		propertyName = (String)value;
    		break;
    	case "dataSignature":
    		signature = (String)value;
    		break;
    	case "dataSignatureGeneric":
    		signatureGeneric = (String)value;
    		if(signatureGeneric != null && signatureGeneric.length() == 0)
    		{
    			signatureGeneric = null;
    		}
    		break;
    	case "humanReadableName":
            humanReadablePropertyName = (String)value;
            if(humanReadablePropertyName != null && humanReadablePropertyName.length() == 0)
            {
                humanReadablePropertyName = null;
            }
    	}
    }
    
    @Override
    public void visitEnd() {
        try {
        	System.out.println("Found compiled property "+className+"/"+propertyName);
            properties.add(new PropertyInfo(
                    className, propertyName, humanReadablePropertyName, signature, signatureGeneric,
                    ACC_PUBLIC, ACC_PUBLIC, true, true
                    ));
        } catch (Exception ex) {
            throw new IllegalArgumentException(ex);
        }
    }
    
}
